package grafica.ventanas;

import grafica.controladores.ControladorEmpresaBaja;
import grafica.controladores.ControladorEmpleadosListado;


import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;

import logica.datatypes.DataAsalariado;
import logica.datatypes.DataEmpleado;
import logica.datatypes.DataEmpresa;
import logica.datatypes.DataFreelance;

import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;


public class EmpleadosListado {

	private JFrame jFrame = null;  //  @jve:decl-index=0:visual-constraint="207,10"
	private JPanel jContentPane = null;
	private JPanel jPanel = null;
	private JTable jTableEmpresas = null;
	private ControladorEmpleadosListado ConEmpleadoListado;
	private JPanel jPanel1 = null;
	private JButton jButton = null;
	private DataEmpresa[] Empresas ;
	private int [] IdEmpresas;
	private int rut;
	public EmpleadosListado(int val) {
		ConEmpleadoListado = new ControladorEmpleadosListado(this);
		rut = val;
	}
	
	public void setVisible (boolean b){	
		this.getJFrame().setVisible(b);
	}

	/**
	 * This method initializes jFrame	
	 * 	
	 * @return javax.swing.JFrame	
	 */
	private JFrame getJFrame() {
		if (jFrame == null) {
			jFrame = new JFrame();
			jFrame.setSize(new Dimension(538, 230));
			jFrame.setTitle("Listado de Empresas");
			jFrame.setContentPane(getJContentPanel());
			jFrame.setLocationRelativeTo(null);	
		}
		return jFrame;
	}

	/**
	 * This method initializes jContentPane	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJContentPanel() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJPanel(), BorderLayout.NORTH);
			//jContentPane.add(getJTableEmpresas(), BorderLayout.CENTER);
			jContentPane.add(getJPanel1(), BorderLayout.NORTH);
			jContentPane.add(getJButton(), BorderLayout.CENTER);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jPanel = new JPanel();
			jPanel.setLayout(new GridBagLayout());
		}
		return jPanel;
	}



	/**
	 * This method initializes jPanel1	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.fill = GridBagConstraints.BOTH;
			gridBagConstraints2.gridy = 0;
			gridBagConstraints2.weightx = 1.0;
			gridBagConstraints2.weighty = 1.0;
			gridBagConstraints2.gridheight = 1;
			gridBagConstraints2.gridx = 0;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.fill = GridBagConstraints.BOTH;
			gridBagConstraints.gridy = 0;
			gridBagConstraints.weightx = 1.0;
			gridBagConstraints.weighty = 1.0;
			gridBagConstraints.gridx = 0;
			jPanel1 = new JPanel();
			jPanel1.setLayout(new GridBagLayout());
			
			jPanel1.add(getJTableEmpresas(rut), gridBagConstraints2);
		}
		return jPanel1;
	}
	
	/**
	 * This method initializes jTableEmpresas	
	 * 	
	 * @return javax.swing.JTable	
	 */
	private JTable getJTableEmpresas(int rut) {
	
			CargarEmpresas(rut);
			
		
		return jTableEmpresas;
	}	
	
	private void CargarEmpresas(int rut){
		DataEmpleado[] Empleados = ConEmpleadoListado.listEmpleados(rut);
		int cantEmp = Empleados.length;
		int  j ;		
		
		Object[] data = new Object[8];
		DefaultTableModel dtm = new DefaultTableModel();		   	       
		String[] columnas={"RUT","CEDULA","NOMBRE","APELLIDO","EDAD","CANT HS","PRECIO H","SALDO"};

		for(int column = 0; column < 8; column++){
			dtm.addColumn(columnas[column]);
			data[column] = columnas[column];
		}					
		
		dtm.addRow(data);		
		
		for (j=0;j<cantEmp;j++){	
			data[0] =   Empleados[j].getRutEmpresa();
			data[1] =  Empleados[j].getCi();
			data[2] =  Empleados[j].getNombre();
			data[3] =  Empleados[j].getApellido();
			data[4] =  Empleados[j].getEdad();
		if(Empleados[j] instanceof DataFreelance){	
				data[5] =  ((DataFreelance)Empleados[j]).getCantidad_horas();
				data[6] =  ((DataFreelance)Empleados[j]).getPrecio();
				data[7] =  "";	
				                
		}else{
			data[5] =  "";
			data[6] =  "";
			data[7] =  ((DataAsalariado)Empleados[j]).getSalario();
				
				}
		dtm.addRow(data);		
		}
		
		// insertamos el contenido de las columnas	 

		jTableEmpresas = new JTable(dtm);		 
		
	
		jTableEmpresas.setPreferredScrollableViewportSize(new Dimension(400, 70));
		jTableEmpresas.setBackground(new Color(0,0,0,0));  
		//JScrollPane scrollPane = new JScrollPane(jTableEmpresas);
		jTableEmpresas.setPreferredSize(new java.awt.Dimension(200,150));	
	}	

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setText("Volver");
			jButton.setPreferredSize(new java.awt.Dimension(90,25));
			ActionListener BotonVolver = new ActionListener(){
				public void actionPerformed (ActionEvent e){ 				
					jFrame.dispose();				
				}
			};
			jButton.addActionListener(BotonVolver);
		}
		return jButton;
	}


		
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
